package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.api.dto.ApiCallBackTestDTO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/10/12 14:16
 * @since
 * @version
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/apitest")
public class ApiCallBackTestController {
    private static Logger LOG = LoggerFactory.getLogger(ApiCallBackTestController.class);

    @RequestMapping(value = "/callback", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callback(@RequestBody @Valid ApiCallBackTestDTO apiCallBackTestDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            LOG.info("/apitest/callback get response= " + JSON.toJSONString(apiCallBackTestDTO));
            response = new XbbResponse<>(new BaseVO());
        }
        return JSON.toJSONString(response);
    }
}
